package com.sdy.hospitalguard.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.hospitalguard.biz.dto.MatchingPeopleDTO;
import com.sdy.hospitalguard.biz.mapper.TemporaryCardMapper;
import com.sdy.hospitalguard.biz.model.MatchingPeople;
import com.sdy.hospitalguard.biz.model.TemporaryCard;
import com.sdy.hospitalguard.biz.service.InpatientAreaService;
import com.sdy.hospitalguard.biz.service.MatchingPeopleService;
import com.sdy.hospitalguard.biz.service.TemporaryCardService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Date;

/**
 * <p>
 * 临时卡表 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-06-02
 */
@Slf4j
@Service
public class TemporaryCardServiceImpl extends BaseServiceImpl<TemporaryCard> implements TemporaryCardService {
    @Autowired
    private TemporaryCardMapper temporaryCardMapper;
    @Autowired
    private MatchingPeopleService matchingPeopleService;
    @Autowired
    private InpatientAreaService inpatientAreaService;

    @Override
    public Page pageData(Page page, String nameAndPhone, Integer status) {
        return temporaryCardMapper.pageData(page, nameAndPhone, status);
    }

    @Override
    public List listData(String nameAndPhone, Integer status) {
        return temporaryCardMapper.listData(nameAndPhone, status);
    }

    @Override
    public void insertCard(TemporaryCard temporaryCard) throws BizException {
        save(temporaryCard.setCreateTime(new Date()).setStatus(0));
        // 调用人员新增方法
        MatchingPeople matchingPeople = new MatchingPeople();
        matchingPeople.setUserId((1 << 30) | temporaryCard.getId());
        matchingPeople.setName(temporaryCard.getName());
        matchingPeople.setCardId(temporaryCard.getCardId());
        matchingPeople.setRemark("临时卡");
        matchingPeople.setTimeTemplate(temporaryCard.getCreateTime().getTime()/1000 + "-" + temporaryCard.getExpireTime().getTime()/1000);
        matchingPeople.setFaceId((1 << 30) | temporaryCard.getId());
        matchingPeople.setPhotoPath(null);
        matchingPeople.setBuilding(inpatientAreaService.getById(temporaryCard.getAreaId()).getBuildingId().toString());
        matchingPeople.setLastChange(String.valueOf(System.currentTimeMillis()/1000));
        matchingPeople.setStatus(0);
        matchingPeopleService.add(matchingPeople);
    }

    @Override
    public void cardFailure() throws BizException{
        Date date = new Date();
        QueryWrapper<TemporaryCard> wrapper = new QueryWrapper<>();
        wrapper.ne("status", 2);
        wrapper.lt("expire_time", date);
        List<TemporaryCard> list = list(wrapper);
        Integer size = list.size();
        for (int i=0; i<size; i++) {
            deleteCard(list.get(i));
        }
    }

    @Override
    public void deleteCard(TemporaryCard temporaryCard) throws BizException{
        MatchingPeopleDTO matchingPeopleDTO = new MatchingPeopleDTO();
        matchingPeopleDTO.setUserId((1 << 30) | temporaryCard.getId());
        matchingPeopleDTO.setBuilding(inpatientAreaService.getById(temporaryCard.getAreaId()).getBuildingId().toString());
        boolean success = matchingPeopleService.delete(matchingPeopleDTO);
        if (success) {
            update(new UpdateWrapper<TemporaryCard>()
                    .eq("id", temporaryCard.getId()).set("status", 2));
        }
    }

    @Override
    public void changeStatus(TemporaryCard temporaryCard) throws BizException {
        if (temporaryCard.getStatus() == 0) {
            MatchingPeopleDTO matchingPeopleDTO = new MatchingPeopleDTO();
            matchingPeopleDTO.setUserId((1 << 30) | temporaryCard.getId());
            matchingPeopleDTO.setBuilding(inpatientAreaService.getById(temporaryCard.getAreaId()).getBuildingId().toString());
            boolean success = matchingPeopleService.delete(matchingPeopleDTO);
            if (success) {
                update(new UpdateWrapper<TemporaryCard>()
                        .eq("id", temporaryCard.getId())
                        .eq("status", 0).set("status", 1));
            }
        } else {
            MatchingPeople matchingPeople = new MatchingPeople();
            matchingPeople.setUserId((1 << 30) | temporaryCard.getId());
            matchingPeople.setName(temporaryCard.getName());
            matchingPeople.setCardId(temporaryCard.getCardId());
            matchingPeople.setRemark("临时卡");
            matchingPeople.setTimeTemplate(temporaryCard.getCreateTime().getTime()/1000 + "-" + temporaryCard.getExpireTime().getTime()/1000);
            matchingPeople.setFaceId((1 << 30) | temporaryCard.getId());
            matchingPeople.setPhotoPath(null);
            matchingPeople.setBuilding(inpatientAreaService.getById(temporaryCard.getAreaId()).getBuildingId().toString());
            matchingPeople.setLastChange(String.valueOf(System.currentTimeMillis()/1000));
            matchingPeople.setStatus(0);
            boolean success = matchingPeopleService.add(matchingPeople);
            if (success) {
                update(new UpdateWrapper<TemporaryCard>()
                        .eq("id", temporaryCard.getId())
                        .eq("status", 1).set("status", 0));
            }
        }
    }

}
